package com.dxq.test.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.SPUtils;
import com.dxq.test.common.AppManager;
import com.dxq.test.common.Constant;
import com.dxq.test.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Author: zsh
 * Date: 2023/11/13
 * Describe: 欢迎页面
 */
public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener {

    private int cont = 2;//倒计时

    private TextView tvTime;

    private Timer timer = new Timer();

    private Handler handler;
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_welcome);
        initView();

        timer.schedule(task, 1000, 1000);//等待时间一秒，停顿时间一秒

        /**
         * 正常情况下不点击跳过
         */
        handler = new Handler();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                //从闪屏界面跳转到首界面
                //获取登录人信息
                String loginUsername = SPUtils.getInstance().getString("loginUsername", "");
                if (loginUsername.equals("")) {
                    Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    startActivity(intent);
                }
                finish();
            }
        }, cont * 1000);//延迟3S后发送handler信息
    }

    private void initView() {
        tvTime = this.findViewById(R.id.tvTime);
        tvTime.setOnClickListener(this);
        tvTime.setText("跳过 " + cont + "s");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tvTime:
                //从闪屏界面跳转到首界面
                //获取登录人信息
                startNew();
                if (runnable != null) {
                    handler.removeCallbacks(runnable);
                }
                timer.cancel();
                break;

        }
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() { // UI thread
                @Override
                public void run() {
                    cont--;
                    System.out.println("main count=" + cont);
                    tvTime.setText("跳过 " + cont + "s");
                    if (cont < 0) {
                        timer.cancel();
                        tvTime.setVisibility(View.GONE);//倒计时到0隐藏字体
                    }
                }
            });
        }
    };



    Handler handler1 = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                cont--;
                System.out.println("main count=" + cont);
                if (cont == 0) {
                    startNew();
                } else {
                    tvTime.setText(cont + "s");
                    //handler.sendEmptyMessageDelayed(1,1000);
                }
            }
        }
    };

    private void startNew() {
        //获取登录人信息
        String loginUsername = SPUtils.getInstance().getString(Constant.loginUsername, "");
        if (loginUsername.equals("")) {
            Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(intent);
        }
        finish();
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(handler!=null){
            handler.removeCallbacks(runnable);
        }
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }
}
